package bb.lanxing.util;

import java.util.ArrayList;
import java.util.List;

import com.tbruyelle.rxpermissions3.Permission;
import com.tbruyelle.rxpermissions3.RxPermissions;

import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.os.Build;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.device.sgdevcie.tool.PermissionConstants;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.utils.IntentUtils;
import bb.lanxing.view.BiciAlertDialogBuilder;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class RxPermissionUtil {
    static RxPermissionUtil instance;
    Disposable disposable;
    String[] permissions;

    public static String getReadPhoneStatePermission() {
        return "android.permission.READ_PHONE_STATE";
    }

    public static RxPermissionUtil getInstance() {
        if (instance == null) {
            instance = new RxPermissionUtil();
        }
        return instance;
    }

    public static boolean checkUserAgreement() {
        return SharedManager.getInstance().getUserId() != 0 || SharedManager.getInstance().isUseAgreementAgree(1);
    }

    public static String[] getStoragePermissions() {
        if (Build.VERSION.SDK_INT >= 33) {
            return PermissionConstants.PERMISSIONS_STORAGE_REQUIRED_T;
        } else if (Build.VERSION.SDK_INT >= 31) {
            return PermissionConstants.PERMISSIONS_STORAGE_REQUIRED_S;
        }
        return PermissionConstants.PERMISSIONS_STORAGE_REQUIRED;
    }

    public static String getReadStoragePermission() {
        return Build.VERSION.SDK_INT >= 33 ? "android.permission.READ_MEDIA_IMAGES"
                : "android.permission.READ_EXTERNAL_STORAGE";
    }

    public static String[] getSportPermissions() {
        if (Build.VERSION.SDK_INT >= 33) {
            return PermissionConstants.PERMISSIONS_LOCATION_SPORT_REQUIRED_T;
        } else if (Build.VERSION.SDK_INT >= 31) {
            return PermissionConstants.PERMISSIONS_LOCATION_SPORT_REQUIRED_S;
        }
        return PermissionConstants.PERMISSIONS_LOCATION_SPORT_REQUIRED;
    }

    public static String[] getBluetoothPermissions() {
        if (Build.VERSION.SDK_INT >= 31) {
            if (isHarmonyOs()) {
                return PermissionConstants.PERMISSIONS_LOCATION_BLUETOOTH_REQUIRED_HM3;
            }
            return PermissionConstants.PERMISSIONS_BLUETOOTH_REQUIRED_S;
        }
        return PermissionConstants.PERMISSIONS_LOCATION_BLUETOOTH_REQUIRED;
    }

    public static String[] getInspectorPermissions() {
        if (Build.VERSION.SDK_INT >= 33) {
            return PermissionConstants.INSPECTOR_PERMISSION_REQUIRED_T;
        } else if (Build.VERSION.SDK_INT >= 31) {
            return PermissionConstants.INSPECTOR_PERMISSION_REQUIRED_S;
        }
        return PermissionConstants.INSPECTOR_PERMISSION_REQUIRED;
    }

    public static String[] getCameraPermission() {
        if (Build.VERSION.SDK_INT >= 33) {
            return PermissionConstants.PERMISSIONS_CAMERA_STORAGE_REQUIRED_T;
        } else if (Build.VERSION.SDK_INT >= 31) {
            return PermissionConstants.PERMISSIONS_CAMERA_STORAGE_REQUIRED_S;
        }
        return PermissionConstants.PERMISSIONS_CAMERA_STORAGE_REQUIRED;
    }

    public static String[] getLocationOnlyPermission() {
        return PermissionConstants.PERMISSIONS_LOCATION_ONLY_REQUIRED;
    }

    public static String[] getLocationStoragePermission() {
        if (Build.VERSION.SDK_INT >= 33) {
            return PermissionConstants.PERMISSIONS_LOCATION_REQUIRED_T;
        } else if (Build.VERSION.SDK_INT >= 31) {
            return PermissionConstants.PERMISSIONS_LOCATION_BLUETOOTH_REQUIRED_S;
        }
        return PermissionConstants.PERMISSIONS_LOCATION_BLUETOOTH_REQUIRED;
    }

    public static boolean isOpenLocService(final Context context) {
        boolean z;
        boolean z2;
        if (context != null) {
            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            if (locationManager != null) {
                z2 = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                z = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            } else {
                z = false;
                z2 = false;
            }
            if (z2 || z) {
                return true;
            }
        }
        return false;
    }

    public static boolean isHarmonyOs() {
        try {
            Class<?> cls = Class.forName("com.huawei.system.BuildEx");
            return "Harmony".equalsIgnoreCase(cls.getMethod("getOsBrand").invoke(cls).toString());
        } catch (Throwable unused) {
            return false;
        }
    }

    public RxPermissionUtil setPermissions(String... permissions) {
        this.permissions = permissions;
        filterNullPermission();
        return instance;
    }

    private void filterNullPermission() {
        ArrayList<String> arrayList = new ArrayList<>();
        for (String str : this.permissions) {
            if (str != null) {
                arrayList.add(str);
            }
        }
        this.permissions = arrayList.toArray(new String[0]);
    }

    public boolean checkPermission(final Context activity, String... permissions) {
        for (String str : permissions) {
            if (str != null && !str.isEmpty() && ContextCompat.checkSelfPermission(activity, str) == -1) {
                return false;
            }
        }
        return true;
    }

    public boolean checkPermission(BaseActivity activity, String[] permissions) {
        for (String str : permissions) {
            if (str != null && !str.isEmpty() && ContextCompat.checkSelfPermission(activity, str) == -1) {
                return false;
            }
        }
        return true;
    }

    public List<String> getNotHadPermissions(Activity activity, String[] permissions) {
        List<String> notHadPermissions = new ArrayList<>();
        for (String str : permissions) {
            if (str != null && !str.isEmpty() && ContextCompat.checkSelfPermission(activity, str) == -1) {
                if (str.equals("android.permission.ACCESS_COARSE_LOCATION")) {
                    str = activity.getString(R.string.permission_network_location);
                } else if (str.equals("android.permission.ACCESS_FINE_LOCATION")) {
                    str = activity.getString(R.string.permission_gps_location);
                } else if (str.equals("android.permission.READ_PHONE_STATE")) {
                    str = activity.getString(R.string.permission_call_status);
                } else if (str.equals("android.permission.READ_EXTERNAL_STORAGE")) {
                    str = activity.getString(R.string.permission_read_external_storage);
                } else if (str.equals("android.permission.WRITE_EXTERNAL_STORAGE")) {
                    if (Build.VERSION.SDK_INT >= 31) {
                        continue;
                    } else {
                        str = activity.getString(R.string.permission_write_external_storage);
                    }
                } else if (str.equals("android.permission.ACTIVITY_RECOGNITION")) {
                    str = activity.getString(R.string.permission_body_sensor);
                }
                notHadPermissions.add(str);
            }
        }
        return notHadPermissions;
    }

    public boolean checkPermission(Activity activity, String[] permissions) {
        for (String str : permissions) {
            if (str != null && !str.isEmpty() && ContextCompat.checkSelfPermission(activity, str) == -1) {
                return false;
            }
        }
        return true;
    }

    public void permissionRequestEachCombined(final FragmentActivity activity, final PermissionCallBack callback) {
        new RxPermissions(activity).requestEachCombined(this.permissions).subscribe(new Observer<>() {
            @Override
            public void onComplete() {
            }

            @Override
            public void onSubscribe(Disposable d) {
                RxPermissionUtil.this.disposable = d;
            }

            @Override
            public void onNext(Permission permission) {
                if (permission.granted) {
                    callback.granted();
                } else if (permission.shouldShowRequestPermissionRationale) {
                    callback.deny();
                } else {
                    new BiciAlertDialogBuilder(activity).setTitle(R.string.permission_prompt)
                            .setMessage(R.string.permission_basic)
                            .setPositiveButton(R.string.dialog_btn_goto_setting,
                                    (dialog, which) -> IntentUtils.gotoPermissionSetting(activity))
                            .setNegativeButton(R.string.dialog_btn_cancel, null).show();
                }
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }
        });
    }

    public interface PermissionCallBack {
        void deny();

        void granted();
    }
}